package com.ruoyi.standingbook.evaluate.service.impl;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.standingbook.evaluate.domain.EvaluateImportDetail;
import com.ruoyi.standingbook.evaluate.domain.bo.EvaluateImportDetailBo;
import com.ruoyi.standingbook.evaluate.domain.vo.EvaluateImportDetailVo;
import com.ruoyi.standingbook.evaluate.mapper.EvaluateImportDetailMapper;
import com.ruoyi.standingbook.evaluate.service.IEvaluateImportDetailService;

import cn.hutool.core.bean.BeanUtil;
import lombok.RequiredArgsConstructor;

/**
 * 市环境测评导入明细Service业务层处理
 *
 * @author cc
 * @date 2023-04-27
 */
@RequiredArgsConstructor
@Service
public class EvaluateImportDetailServiceImpl implements IEvaluateImportDetailService {

    private final EvaluateImportDetailMapper baseMapper;

    /**
     * 查询市环境测评导入明细
     */
    @Override
    public EvaluateImportDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询市环境测评导入明细列表
     */
    @Override
    public TableDataInfo<EvaluateImportDetailVo> queryPageList(EvaluateImportDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EvaluateImportDetail> lqw = buildQueryWrapper(bo);
        Page<EvaluateImportDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询市环境测评导入明细列表
     */
    @Override
    public List<EvaluateImportDetailVo> queryList(EvaluateImportDetailBo bo) {
        LambdaQueryWrapper<EvaluateImportDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EvaluateImportDetail> buildQueryWrapper(EvaluateImportDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EvaluateImportDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getBatchNo()), EvaluateImportDetail::getBatchNo, bo.getBatchNo());
        lqw.between(params.get("beginEvaluateTime") != null && params.get("endEvaluateTime") != null,
            EvaluateImportDetail::getEvaluateTime ,params.get("beginEvaluateTime"), params.get("endEvaluateTime"));
        lqw.between(params.get("beginPublishDate") != null && params.get("endPublishDate") != null,
            EvaluateImportDetail::getPublishDate ,params.get("beginPublishDate"), params.get("endPublishDate"));
        lqw.eq(bo.getProcessStatus() != null, EvaluateImportDetail::getProcessStatus, bo.getProcessStatus());
        return lqw;
    }

    /**
     * 新增市环境测评导入明细
     */
    @Override
    public Boolean insertByBo(EvaluateImportDetailBo bo) {
        EvaluateImportDetail add = BeanUtil.toBean(bo, EvaluateImportDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改市环境测评导入明细
     */
    @Override
    public Boolean updateByBo(EvaluateImportDetailBo bo) {
        EvaluateImportDetail update = BeanUtil.toBean(bo, EvaluateImportDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(EvaluateImportDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除市环境测评导入明细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

	@Override
	public Boolean batchInsertVo(List<EvaluateImportDetailVo> voList) {
		List<EvaluateImportDetail> list= BeanCopyUtils.copyList(voList , EvaluateImportDetail.class);
		return baseMapper.insertBatch(list);
	}
}
